package com.tangly.codeing.controller;

import com.tangly.codeing.model.Article;
import com.tangly.codeing.repository.ArticleRepository;
import com.tangly.codeing.service.ArticleService;
import com.tangly.codeing.utils.PageInfo;
import com.tangly.codeing.utils.ReturnT;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 文章
 *
 * @author tangbing
 * @date 2019-10-30
 */
@RestController
@RequestMapping("article")
public class ArticleController extends BaseController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleRepository articleRepository;

    /**
     * 新增
     */
    @PostMapping("/insert")
    public ReturnT<String> insert(@RequestBody Article article) {
        return articleService.insert(article);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    public ReturnT<String> delete(@PathVariable("id") Long id) {
        return articleService.delete(id);
    }

    /**
     * 批量删除
     */
    @GetMapping("/batchDelete")
    public ReturnT<String> batchDelete(@RequestParam("ids") String ids) {
        String[] split = ids.split(",");
        return articleService.batchDelete(Arrays.asList(split));
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    public ReturnT<String> update(@RequestBody Article article) {
        return articleService.update(article);
    }

    /**
     * Load查询
     */
    @GetMapping("/load/{id}")
    public ReturnT<Object> load(@PathVariable("id") Long id) {
        return new ReturnT<>(articleService.load(id));
    }

    /**
     * 分页查询
     */
    @PostMapping("/pageList")
    public ReturnT<Object> pageList(@RequestBody PageInfo pageInfo) {
        ReturnT<Object> validateRes = validateParam(pageInfo);
        if (validateRes != null) {
            return validateRes;
        }
        return new ReturnT<>(articleService.pageList(pageInfo));
    }

    /**
     * es根据id查询
     */
    @PostMapping("/esQueryArticleById")
    public ReturnT<Object> esPageList(@RequestParam Long id) {
        Article article = articleRepository.queryArticleById(id);
        return new ReturnT<>(article);
    }
}
